package role

import (
	"reflect"
	codecs "sanguo/codec/cs"
	"sanguo/node/node_map"
	"sanguo/node/node_map/scene"
	cs_message "sanguo/protocol/cs/message"
	ss_rpc "sanguo/protocol/ss/rpc"
	"sanguo/rpc/changeOutline"

	"github.com/golang/protobuf/proto"
)

type ChangeOutline struct {
}

func (this *ChangeOutline) OnCall(replyer *changeOutline.ChangeOutlineReplyer, arg *ss_rpc.ChangeOutlineReq) {

	//node_map.Infoln("OnCall ChangeOutline", arg)
	scene_ := scene.GetSceneByType(scene.SceneType(arg.GetMapType()))

	if nil == scene_ {
		replyer.Reply(&ss_rpc.ChangeOutlineResp{
			OK: proto.Int32(0),
		})
		return
	}

	scene_.PostTask(func() {

		sceneO := scene_.GetObjByID(arg.GetUserID())
		if nil == sceneO {
			replyer.Reply(&ss_rpc.ChangeOutlineResp{
				OK: proto.Int32(0),
			})
			return
		}

		sceneO.(*Role).Lev = arg.GetLev()
		sceneO.(*Role).Weapon1 = arg.GetWeapon1()
		sceneO.(*Role).Weapon2 = arg.GetWeapon2()

		sceneO.(*Role).NotifyChangeOutline()

		replyer.Reply(&ss_rpc.ChangeOutlineResp{
			OK: proto.Int32(1),
		})
	})
}

func (this *Role) NotifyChangeOutline() {

	msg := &cs_message.ChangeOutlineToC{
		RoleID:  proto.Uint64(this.ID),
		Lev:     proto.Int32(this.Lev),
		Weapon1: proto.Int32(this.Weapon1),
		Weapon2: proto.Int32(this.Weapon2),
	}
	msgType := reflect.TypeOf(msg).String()
	bytes, err := encoder.EnCode(codecs.NewMessage(0, msg))

	if err != nil {
		node_map.Infoln("OnAoiChangeOutline err", err)
		return
	}

	this.SendViewObjToClient(msgType, bytes.Bytes())
}

func init() {
	changeOutline.Register(&ChangeOutline{})
}
